﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Workflows.Conditions
{
    public sealed class ConditionCollectionCondition : TransitionCondition
    {
        public ConditionCollectionCondition(ConditionCollection coll)
        {
            if (coll == null)
                throw new ArgumentNullException("coll");

            _collection = coll;
        }

        private ConditionCollection _collection;

        public ConditionCollection Collection
        {
            get { return _collection; }
        }


        protected internal override bool Test(ConditionTestArguments args)
        {
            return Test(_collection, args);
        }

        public static bool Test(ConditionCollection collection, ConditionTestArguments args)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");

            bool match;
            int count = 0;
            ConditionRelation rel = collection.ItemsRelation;

            foreach (var tc in collection)
            {
                if (tc.Test(args))
                {
                    count++;
                }
                else if (rel == ConditionRelation.And)
                    break;
            }

            if (rel == ConditionRelation.And)
                match = count == collection.Count;
            else
                match = count > 0;

            return match;
        }

        protected internal override void PopulateDependentProperties(List<string> list)
        {
            foreach (var cond in _collection)
            {
                cond.PopulateDependentProperties(list);
            }
        }
    }
}
